// ==UserScript==
// @name         去广告
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  去除斐讯和部分页面流氓广告
// @author       You
// @match        http*://*/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    var url = window.location.href;

    var closeList = [/\/\/p\.ynb2dca\.com/];
    var scriptList = [/\/\/45\.126\.123\.80/,/\/\/p\.ynb2dca\.com/];
    var hrefList = [/\/\/p\.ynb2dca\.com/];
    var cssList = [
        {url: /www\.pushbta\.com/, cssDes: ["#__lgUnion_a__12", "#ETE", "#cs_kd_div"]},
        {url:/zhongzishenqi\.me/,cssDes:[".οsd","iframe[src~='i.mzxun.com']","#LELE_ShowDIV_UNIONtop","#__jclm_right_couplet","#__jclm_left_couplet","#__jclm_r_b_59145"]}
    ];

    var adsHelper = (function () {
        function closePage() {
            for (let i = 0; i < closeList.length; i++) {
                // console.error(closeList[i].test(url));
                closeList[i].test(url) ? window.close() : "";
                break;
            }
        }

        //清除所有插入的a跳转
        function removeHref() {
            let elList = document.querySelectorAll("a");

            for (let i = 0; i < hrefList; i++) {
                for (let j = 0; j < elList.length; j++) {
                    let el = elList[j];
                    if (hrefList[i].test(el.href)) {
                        el.parentNode.removeChild(el);
                    }
                }
            }
        }

        //清除script
        function removeScript() {
            let elList = document.querySelectorAll("script");

            for (let i = 0; i < scriptList.length; i++) {
                for (let j = 0; j < elList.length; j++) {
                    let el = elList[j];
                    if (scriptList[i].test(el.src)) {
                        el.parentNode.removeChild(el);
                    }
                }

            }
        }

        function removeCssEl() {
            let has = false;
            let cssDes;
            for (let i = 0; i < cssList.length; i++) {
                if (cssList[i].url.test(url)) {
                    has = true;
                    cssDes = cssList[i].cssDes;
                    break;
                }
            }
            if (!has) return;
            for (let i = 0; i < cssDes.length; i++) {
                let el = document.querySelector(cssDes[i]);
                el ? el.parentNode.removeChild(el) : "";
            }
        }

        return {closePage, removeHref, removeScript, removeCssEl}
    })();

    //开始过滤广告
    adsHelper.closePage();
    adsHelper.removeScript();
    adsHelper.removeHref();
    adsHelper.removeCssEl();
})();